package com.be.beadmin.material.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.material.dto.input.PickingOverViewDto;
import com.be.beadmin.material.dto.output.PickingOverViewEx;
import com.be.beadmin.material.service.PickingOverViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zmy
* @date 2023-05-14
**/
@RestController
public class PickingOverViewController {

    @Autowired
    private PickingOverViewService pickingOverViewService;


    @PostMapping(value = "/product/getPickingOverViewByPage")
    public ResultModel getPickingOverViewByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(pickingOverViewService.getPickingOverViewByPage(pageEntity)) ;
    }

    @PostMapping("/product/addPickingOverView")
    public ResultModel addPickingOverView(@RequestBody PickingOverViewDto pickingOverViewDto){
        pickingOverViewService.addPickingOverView(pickingOverViewDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delPickingOverView")
    public ResultModel delPickingOverView(@RequestBody PickingOverViewDto pickingOverViewDto){
        pickingOverViewService.delPickingOverView(pickingOverViewDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updatePickingOverView")
    public ResultModel updatePickingOverView(@RequestBody PickingOverViewDto pickingOverViewDto){
        pickingOverViewService.updatePickingOverView(pickingOverViewDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportPickingOverView")
    public void exportPickingOverView(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PickingOverViewEx> pickingOverViewPage = pickingOverViewService.getPickingOverViewByPage(pageEntity);
        List<PickingOverViewEx> pickingOverViewExes = pickingOverViewPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PickingOverView接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PickingOverViewEx.class).sheet("sheet1").doWrite(pickingOverViewExes);
    }
}